'use strict';

const Service = require('egg').Service;
let Sequelize = require('sequelize');
let Op = Sequelize.Op;

class BookService extends Service {
    // resources  index / create / destroy / update

    // 查询
    async index() {
        try {
            let params = this.ctx.request.query;
            let pageNum = Number(params.currentPage) || 1;
            let pageSize = Number(params.pagesize) || 7;
            // findAndCountAll sequelize 分页查询
            // offset 起始位置 偏移量
            // limit 多少条数据
            let res = []
            if (!params.searchBook) {
                res = await this.app.model.Book.findAndCountAll({
                    offset: pageNum * pageSize - pageSize,
                    limit: pageSize
                }
                );
                // 判断 当删除最后一页的最后一条数据时 并且不是最后一条  获取最后一页前一页的内容
                if (res.rows.length == 0 && res.count > 1) {
                    pageNum = pageNum - 1
                    res = await this.app.model.Book.findAndCountAll({
                        offset: pageNum * pageSize - pageSize,
                        limit: pageSize
                    });
                }
            } else {
                res = await this.app.model.Book.findAndCountAll({
                    offset: pageNum * pageSize - pageSize,
                    limit: pageSize,
                    where: {
                        name: {
                            [Op.like]: '%' + params.searchBook + '%'
                        }
                    }
                });
            }
            return res
        } catch (e) { console.log(e) }
    }
    // 查询书籍类目
    async getclazz() {
        try {
            let res = await this.app.model.book.findAll({
                include: { model: this.app.model.Clazz }
            });
            return res
        } catch (e) { console.log(e) }
    }
    // 搜索
    async search(params) {
        try {
            // let pageNum = Number(params.currentPage) || 1;
            // let pageSize = Number(params.pagesize) || 10;
            let res = await this.app.model.Book.findAndCountAll({
                // offset: pageNum * pageSize - pageSize,
                // limit: pageSize,
                where: {
                    name: {
                        [Op.like]: '%' + params.searchBook + '%'
                    }
                }
            });
            return res
        } catch (e) { console.log(e) }
    }
    // 添加
    async create() {
        try {
            let book = this.ctx.request.body.book;
            let isCreate = await this.app.model.Book.create({
                name: book.name,
                price: book.price,
                createDate: book.createDate,
                category: book.Category,
                press: book.press,
                author: book.author,
                info: book.info,
                cover: book.cover,
                clazz_id: book.clazz_id
            });
            // affectedRows 成功= 1   失败 = 0
            if (isCreate.affectedRows === 1) {
                return { status: { code: 400, msg: "添加失败" } }
            } else {
                return { status: { code: 200, msg: "添加成功" } }
            }
        } catch (e) { console.log(e) }
    }
    // 删除
    async destroy() {
        try {
            let id = this.ctx.params.id;
            await this.app.model.Book.destroy({
                where: {
                    id
                }
            })
            let res = await this.ctx.service.book.index()
            return res
        } catch (e) { console.log(e) }
    }
    // 修改
    async update() {
        try {
            let id = this.ctx.params.id;
            let book = this.ctx.request.body.book;
            let cover = this.ctx.request.body.cover;
            await this.app.model.Book.update({
                name: book.name,
                price: book.price,
                category: book.Category,
                createDate: book.createDate,
                press: book.press,
                author: book.author,
                info: book.info,
                cover: cover,
                clazz_id: book.clazz_id
            }, {
                where: {
                    id
                }
            })
            let res = await this.ctx.service.book.index()
            return res
        } catch (err) { console.log(err) }
    }
}

module.exports = BookService;
